import 'dart:convert';

import 'package:xindong/common/xindong_types.dart';
import 'package:xindong/models/chat_message/xindong_custom_message.dart';
import 'package:xindong/models/chat_message/xindong_signal_message.dart';
import 'package:xindong/utils/xindong_conversion_util.dart';
import 'package:tencent_cloud_chat_uikit/tencent_cloud_chat_uikit.dart';

import '../../common/xindong_methods.dart';

// 自定义消息类型
class XinDongCustomMessageType {
  static const none = 0; // 无

  static const gift = 11000; // 礼物

  static const int balance = 12000; // 用户余额变化
  static const int anchorAutoCall = 12100; // 通知主播自动呼叫
  static const int anchorAutoGreet = 12101; // 通知主播自动打招呼
  static const int talkStart = 12200; // 通知双方通话正式开始，扣费生效
  static const int talkFinish = 12300; // 通知双方通话结束

  static const restartApp = 13000; // 重启应用
  static const dialog = 14000; // 弹窗
  static const updateUserInfo = 15000; // 更新用户信息

  static const updateSeenMe = 15002; // 更新谁看过我

  static const characterRecharge = 16000; // 文字聊充值弹窗
  static const characterVoice = 16001; // 文字聊语音
  static const characterImage = 16002; // 文字聊图片
  static const characterVideo = 16003; // 文字聊视频
  static const characterMessageFreeTimes = 16004; // 文字聊免费消息数
  static const characterMessageWillConsume = 16005; // 文字聊将要付费（有足够余额的才提醒）
  static const characterClearConversation = 16400; // 文字聊清除会话
  static const characterAnchorInformation = 16999; // 主播资料显示
  static const emotion = 17002; // 表情
  static const gameMessage = 17002; // 小游戏
}

class XinDongChatMessageType {
  // 是否是人发的聊天消息，不是提示消息
  bool isChatType(V2TimMessage message) {
    switch (message.elemType) {
      case MessageElemType.V2TIM_ELEM_TYPE_TEXT:
      case MessageElemType.V2TIM_ELEM_TYPE_IMAGE:
      case MessageElemType.V2TIM_ELEM_TYPE_SOUND:
      case MessageElemType.V2TIM_ELEM_TYPE_VIDEO:
      case MessageElemType.V2TIM_ELEM_TYPE_FILE:
      case MessageElemType.V2TIM_ELEM_TYPE_LOCATION:
      case MessageElemType.V2TIM_ELEM_TYPE_FACE:
        return true;
      case MessageElemType.V2TIM_ELEM_TYPE_GROUP_TIPS:
        return false;
      case MessageElemType.V2TIM_ELEM_TYPE_CUSTOM:
        {
          // 信令提示
          if (isChatTypeSignaling(message)) {
            return false;
            // return true;
          }
          try {
            XinDongCustomMessage? model =
                XinDongConversionUtil.decodeJsonToMessageClass(message.customElem?.data ?? "");
            if (model != null) {
              if (model.type == "${ChatType.gift}") {
                // 居然是字符串
                return true;
              }
              // 文字聊是聊天消息
              switch (model.type) {
                case ChatType.sayHello:
                case ChatType.text:
                case ChatType.voice:
                case ChatType.image:
                case ChatType.video:
                case ChatType.gift:
                case ChatType.callAudio:
                case ChatType.callVideo:
                case ChatType.callEnd:
                case ChatType.callCancel:
                // case ChatType.poor:
                case XinDongCustomMessageType.characterImage:
                case XinDongCustomMessageType.characterVoice:
                case XinDongCustomMessageType.characterVideo:
                case XinDongCustomMessageType.emotion:
                case XinDongCustomMessageType.gift:
                  return true;
                default:
                  return false;
              }
            }
          } catch (e) {
            mCatchPrint(e);
            return false;
          }
        }
        break;
    }
    return false;
  }

  // 是否是人发的信令聊天消息，不是提示消息
  bool isChatTypeSignaling(V2TimMessage message) {
    if (isSignalingMessage(message)) {
      String data = message.customElem!.data!;
      Map<String, dynamic> signalDataMap = json.decode(data);
      int actionType = signalDataMap["actionType"];
      XinDongSignalMessage? model = XinDongConversionUtil.decodeJsonToSignalClass(signalDataMap["data"]);
      switch (actionType) {
        case XinDongSignalActionType.invite:
          return model.isCallEnd ? false : true;
        case XinDongSignalActionType.reject:
          return model.isBusy ? false : true;
        case XinDongSignalActionType.cancel:
          return true;
        case XinDongSignalActionType.timeout:
        case XinDongSignalActionType.accept:
          return false;
      }
    }
    return false;
  }

  // 是不是邀请信令消息
  bool isSignalingMessage(V2TimMessage message) {
    if (message.elemType != MessageElemType.V2TIM_ELEM_TYPE_CUSTOM) return false;

    if (message.customElem?.data == null) return false;

    final String data = message.customElem!.data!;
    if (data.contains("inviteID") && data.contains("inviter") && data.contains("actionType")) {
      return true;
    } else {
      return false;
    }
  }

  bool isExceedThree(String str) {
    RegExp regExp = RegExp(r'(.*[a-zA-Z]){3,}|(.*\d){3,}|(.*[a-zA-Z0-9]){3,}'); // 匹配含有三个或以上字母、数字或者字母数字组合
    return regExp.hasMatch(str); // 如果匹配成功，返回 true
  }

  // 获取消息类型
  // 系统消息 文字 语音 图片 视频 文件 位置 表情 自定义等
  String getMessageType(V2TimMessage? message) {
    if (message == null) {
      return "[无消息]";
    }
    if (message.status == MessageStatus.V2TIM_MSG_STATUS_HAS_DELETED) {
      return "[消息被删除]";
    } else if (message.status == MessageStatus.V2TIM_MSG_STATUS_LOCAL_REVOKED) {
      return "[消息被撤回]";
    }
    String name = "";
    switch (message.elemType) {
      case MessageElemType.V2TIM_ELEM_TYPE_TEXT:
        if (message.textElem?.text != null && isExceedThree(message.textElem?.text ?? "")) {
          name = "[消息]";
          break;
        }
        name = message.textElem == null ? '' : message.textElem!.text ?? '';
        break;
      case MessageElemType.V2TIM_ELEM_TYPE_CUSTOM:
        {
          // 信令提示
          if (isSignalingMessage(message)) {
            return "[通话]";
          }
          name = "[系统消息]"; // 暂时没有自定义消息
          try {
            XinDongCustomMessage? model =
                XinDongConversionUtil.decodeJsonToMessageClass(message.customElem?.data ?? "");
            if (model != null) {
              switch (model.type) {
                case ChatType.daShan:
                  name = "[搭讪红包]";
                  break;
                case ChatType.sayHello:
                case ChatType.text:
                  name = model.content;
                  break;
                case ChatType.callAudio:
                case ChatType.callVideo:
                  name = "[通话]";
                  break;
                case ChatType.callCancel:
                  name = "[${int.parse("${model.chat_type}") == ChatType.callAudio ? "语音" : "视频"}${model.content}]";
                  break;
                case ChatType.callEnd:
                  name = model.duration_text;
                  break;
                case XinDongCustomMessageType.characterImage:
                  name = "[图片]";
                  break;
                case XinDongCustomMessageType.characterVoice:
                  name = "[语音]";
                  break;
                case XinDongCustomMessageType.characterVideo:
                  name = "[视频]";
                  break;
                case XinDongCustomMessageType.emotion:
                  name = "[小游戏]";
                  break;
                case XinDongCustomMessageType.gift:
                  name = "[礼物]";
                  break;
                default:
                  break;
              }
            }
          } catch (e) {
            mCatchPrint(e);
          }
        }
        break;
      case MessageElemType.V2TIM_ELEM_TYPE_IMAGE:
        name = "[图片]";
        break;
      case MessageElemType.V2TIM_ELEM_TYPE_SOUND:
        name = "[语音]";
        break;
      case MessageElemType.V2TIM_ELEM_TYPE_VIDEO:
        name = "[视频]";
        break;
      case MessageElemType.V2TIM_ELEM_TYPE_FILE:
        name = "[文件]";
        break;
      case MessageElemType.V2TIM_ELEM_TYPE_LOCATION:
        name = "[定位]";
        break;
      case MessageElemType.V2TIM_ELEM_TYPE_FACE:
        name = "[表情]";
        break;
      case MessageElemType.V2TIM_ELEM_TYPE_GROUP_TIPS:
        name = "[系统通知]";
        break;
      default:
        name = "[New message]";
        break;
    }
    return name;
  }
}
